﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Systems.Entity;
using ShareMarking.Systems.Service;
using System.Security.Cryptography;
using System.Text;

namespace ShareMarking.Web.admin
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtUserName.Text = Utils.GetCookie("DTRememberName");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string userPwd = txtPassword.Text.Trim();

            if (userName.Equals("") || userPwd.Equals(""))
            {
                msgtip.InnerHtml = "请输入用户名或密码";
                return;
            }

            if ((Session["CheckCode"] == null || txtCheckCode.Text.ToLower() != Session["CheckCode"].ToString().ToLower()))
            {
                msgtip.InnerHtml = "验证码无效，请确认您输入的验证码有效！";
                return;
            }

            if (Session["AdminLoginSun"] == null)
            {
                Session["AdminLoginSun"] = 1;
            }
            else
            {
                Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
            }
            //判断登录错误次数
            if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 5)
            {
                msgtip.InnerHtml = "错误超过5次，关闭浏览器重新登录！";
                return;
            }
            sys_manager model = null;

            //先取得该用户的随机密钥
            var manager = sys_managerService.Instance.Get_SingleEntity_byWhere(sys_manager._.user_name == userName);
            if (manager != null && string.IsNullOrEmpty(manager.salt))
            {
                model = null;
            }
            else
            {
                //把明文进行加密重新赋值
                var password = DESEncrypt.Encrypt(userPwd, manager.salt);
                model = sys_managerService.Instance.Get_SingleEntity_byWhere(sys_manager._.user_name == userName && sys_manager._.password == password);
            }
            
            if (model == null)
            {
                msgtip.InnerHtml = "用户名或密码有误，请重试！";
                return;
            }
            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            Session.Timeout = 45;
            //写入登录日志
            siteconfig siteConfig = siteconfigService.Instance.loadConfig();
            if (siteConfig.logstatus > 0)
            {
                sys_manager_logService.Instance.Add_Entity(new sys_manager_log()
                    {
                        user_id = model.id,
                        user_name = model.user_name,
                        action_type = DTEnums.ActionEnum.Login.ToString(),
                        remark = "用户登录",
                        add_time = DateTime.Now,
                        user_ip = Request.UserHostAddress
                    });
            }
            //写入Cookies
            Utils.WriteCookie("DTRememberName", model.user_name, 14400);
            Utils.WriteCookie("AdminName", "DTcms", model.user_name);
            Utils.WriteCookie("AdminPwd", "DTcms", model.password);
            Response.Redirect("index.aspx");
            return;
        }

    }
}